package com.umakr.ax.app.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.umakr.ax.common.model.UserDetails;
import com.umakr.ax.common.model.UserRelation;
import com.umakr.ax.common.service.UserDetailsService;
import com.umakr.ax.common.service.UserRelationService;
import com.umakr.ax.security.model.User;
import com.umakr.ax.security.model.UserRole;
import com.umakr.ax.security.service.UserRoleService;
import com.umakr.ax.security.service.UserService;
import com.umakr.ax.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author tangtengfei
 */
@Service
public class UserWebService {

    @Autowired
    UserService userService;
    @Autowired
    UserDetailsService userDetailsService;
    @Autowired
    UserRoleService userRoleService;
    @Autowired
    UserWebRelationService userRelationService;


    public void addRsAdmin(Long userId,String account, String password, String nickName,Long rsId,Integer roleName,
                           Integer status,Integer type) {
        String rolename;
        Integer roleId;
        if(roleName == 0) {
            rolename = "主播";
            roleId = 4;
        } else {
            rolename = "导播";
            roleId = 5;
        }
        Date date = new Date();
        if(userId == null) {
            User user = new User();
            user.setAccount(account);
            user.setRemark(rolename);
            user.setStatus(status);
            user.setPassword(WebUtil.getBean(BCryptPasswordEncoder.class).encode(password));
            userService.update(user);

            //添加用户明细
            UserDetails userDetails = new UserDetails();
            userDetails.setNickname(nickName);
            userDetails.setUserId(user.getId());
            userDetails.setCreateTime(date);
            userDetailsService.update(userDetails);
            //添加角色
            UserRole userRole = new UserRole();
            userRole.setUserId(user.getId());
            userRole.setRoleId((long) roleId);
            userRoleService.update(userRole);
            //添加用户电台关系表
            UserRelation userRelation = new UserRelation();
            userRelation.setRsId(rsId);
            userRelation.setUserId(user.getId());
            userRelationService.update(userRelation);
        }else {
            //仅修改密码
            if(type == 0) {
                EntityWrapper<User> userWrapper = new EntityWrapper<>();
                userWrapper.eq("id",userId);
                User user = userService.selectOne(userWrapper);
                if(password != null && !"".equals(password)){
                    user.setPassword(WebUtil.getBean(BCryptPasswordEncoder.class).encode(password));
                }
                userService.update(user);
            } else {
                //更新用户表
                EntityWrapper<User> userWrapper = new EntityWrapper<>();
                userWrapper.eq("id",userId);
                User user = userService.selectOne(userWrapper);
                user.setAccount(account);
                if(password != null) {
                    user.setPassword(WebUtil.getBean(BCryptPasswordEncoder.class).encode(password));
                }
                user.setStatus(status);
                user.setRemark(rolename);
                userService.update(user);

                //更新用户详情表
                EntityWrapper<UserDetails> userDetailsWrapper = new EntityWrapper<>();
                userDetailsWrapper.eq("user_id",userId);
                UserDetails userDetails = userDetailsService.selectOne(userDetailsWrapper);
                userDetails.setNickname(nickName);
                userDetailsService.update(userDetails);

                //更新角色
                EntityWrapper<UserRole> userRoleWrapper = new EntityWrapper<>();
                userRoleWrapper.eq("user_id",userId);
                UserRole userRole = userRoleService.selectOne(userRoleWrapper);
                userRole.setRoleId((long)roleId);
                userRoleService.update(userRole);
            }

        }
    }

    public void delectRsAdmin(Long userId) {
        userService.delete(userId);
        userDetailsService.delByUserId(userId);
        userRoleService.delByUserId(userId);
    }
}
